package model;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name="Room")
public class Room  {

	@OneToMany
	private List<Player> players;
	@Id
	private String name;
	private String passwd;
	@OneToOne
	private Player bomb;

	public Room(String name, String passwd) {
		players = new LinkedList<Player>();
		this.name = name;
		this.passwd = passwd;
	}

	public boolean save() {
		try {
			EntityManager em = Persistence.createEntityManagerFactory("defaultPersistenceUnit").createEntityManager();
			em.getTransaction().begin();
			em.persist(this);
			em.getTransaction().commit();
		} catch (Exception e) {
			System.out.println("fail " +e.getMessage());
			return false;
		}
		return true;
	}

	public String getPasswd() {
		return passwd;
	}
	
	public void addPlayer(Player p) {
		this.players.add(p);
		restart();
	}
	
	public void removePlayer(Player p) {
		this.players.remove(p);
	}
	public void restart() {
		
	}

	public boolean hasPlayers() {
		return !this.players.isEmpty();
	}

	public Player getBombPlayer() {
		return bomb;
	}

	public boolean playerIsIn(Player p) {
		return players.contains(p);
	}

}
